<template>
    <div>
      <!--面包学导航-->
      <el-breadcrumb separator="/">
        <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
        <el-breadcrumb-item>商品管理</el-breadcrumb-item>
        <el-breadcrumb-item>商品分类</el-breadcrumb-item>
      </el-breadcrumb>
      <!--卡片部分-->
      <el-card>
        <!--按钮-->
        <el-row>
          <el-col>
            <el-button type="primary" @click="showAddCategoryDialog">添加分类</el-button>
          </el-col>
        </el-row>
        <!--表格-->
        <tree-table class="treeTable" :data="cateList" :columns="columns"
                    :selectionType="false" :expand-type="false"
                    :show-index="true" index-text="#" border :show-row-hover="false"
        >
          <template slot="isOk"  slot-scope="scope">
            <i class="el-icon-success" style="color: lightgreen;"  v-if="scope.row.cat_deleted===false"></i>
            <i class="el-icon-error" style="color: red;"  v-else></i>
          </template>
          <template slot="level" slot-scope="scope">
            <el-tag type="primary" v-if="scope.row.cat_level === 0" size="mini">一级</el-tag>
            <el-tag type="success" v-if="scope.row.cat_level === 1" size="mini">二级</el-tag>
            <el-tag type="warning" v-if="scope.row.cat_level === 2" size="mini">三级</el-tag>
          </template>
          <template slot="behave" slot-scope="scope">
            <el-button type="primary" icon="el-icon-edit" size="mini" @click="showRemoveCateDialog(scope.row)">编辑</el-button>
            <el-button type="danger" icon="el-icon-delete" size="mini" @click="removeCate(scope.row.cat_id)">删除</el-button>
          </template>
        </tree-table>
        <!--分类列表分页部分-->
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="queryInfo.pagenum"
          :page-sizes="[5, 10, 15, 20, 30]"
          :page-size="queryInfo.pagesize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total">
        </el-pagination>
      </el-card>
      <!--添加分类对话框-->
      <el-dialog
        title="添加分类"
        :visible.sync="addCategoryDialogVisible"
        width="50%" @close="addCateDialogClosed">
        <el-form :model="addCateForm" :rules="addCateRules"
                 ref="addCateFormRef" label-width="100px">
          <el-form-item label="分类名称" prop="cat_name">
            <el-input v-model="addCateForm.cat_name"></el-input>
          </el-form-item>
          <el-form-item label="父级分类">
            <el-cascader
              v-model="selectedKeys"
              :options="parentCate"
              :props="cascaderProps"
              clearable change-on-select @change="parentCateChanged">
            </el-cascader>
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button @click="addCategoryDialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="addCate">确 定</el-button>
        </span>
      </el-dialog>
      <!--编辑分类对话框-->
      <el-dialog
        title="编辑分类"
        :visible.sync="removeCateDialogVisible"
        width="50%" @close="removeCateDialogClosed">
        <el-form :model="removeCateForm" :rules="addCateRules"
                 ref="removeCateFormRef" label-width="100px">
          <el-form-item label="分类名称" prop="cat_name">
            <el-input v-model="removeCateForm.cat_name"></el-input>
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button @click="removeCateDialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="reviseCate">确 定</el-button>
        </span>
      </el-dialog>
    </div>
</template>

<script>
export default {
  created () {
    this.getCateList()
  },
  data () {
    return {
      queryInfo: {
        type: 3,
        pagesize: 5,
        pagenum: 1
      },
      cateList: [],
      total: 0,
      columns: [
        {
          label: '分类名称',
          prop: 'cat_name'
        },
        {
          label: '是否有效',
          prop: 'cat_deleted',
          type: 'template',
          template: 'isOk'
        },
        {
          label: '排序',
          prop: 'cat_level',
          type: 'template',
          template: 'level'
        },
        {
          label: '操作',
          type: 'template',
          template: 'behave'
        }
      ],
      addCategoryDialogVisible: false,
      addCateForm: {
        cat_name: '',
        cat_pid: 0,
        cat_level: 0
      },
      addCateRules: {
        cat_name: [
          { required: true, message: '请输入分类名称', trigger: 'blur' }
        ]
      },
      parentCate: [],
      cascaderProps: {
        expandTrigger: 'hover',
        value: 'cat_id',
        label: 'cat_name',
        children: 'children'
      },
      selectedKeys: [],
      removeCateDialogVisible: false,
      removeCateForm: {
        cat_name: ''
      }
    }
  },
  methods: {
    async getCateList () {
      const { data: res } = await this.$http.get('categories', { params: this.queryInfo })
      if (res.meta.status !== 200) return this.$message.error('获取分类列表失败')
      this.cateList = res.data.result
      this.total = res.data.total
      this.$message.success('获取列表成功')
    },
    handleCurrentChange (newPage) {
      this.queryInfo.pagenum = newPage
      this.getCateList()
    },
    handleSizeChange (newSize) {
      this.queryInfo.pagesize = newSize
      this.getCateList()
    },
    async getParentCate () {
      const { data: res } = await this.$http.get('categories', { params: { type: 2 } })
      if (res.meta.status !== 200) return this.$message.error('父级分类获取失败')
      this.parentCate = res.data
      this.$message.success('列表获取成功')
    },
    async showAddCategoryDialog () {
      this.getParentCate()
      this.addCategoryDialogVisible = true
    },
    addCateDialogClosed () {
      this.$refs.addCateFormRef.resetFields()
      this.selectedKeys = []
      this.addCateForm.cat_level = 0
      this.addCateForm.cat_pid = 0
    },
    parentCateChanged () {
      console.log(this.selectedKeys)
      if (this.selectedKeys.length > 0) {
        this.addCateForm.cat_pid = this.selectedKeys[this.selectedKeys.length - 1]
        this.addCateForm.cat_level = this.selectedKeys.length
        console.log(this.addCateForm)
      } else {
        this.addCateForm.cat_pid = 0
        this.addCateForm.cat_level = 0
        console.log(this.addCateForm)
      }
    },
    addCate () {
      this.$refs.addCateFormRef.validate(async valid => {
        if (!valid) return
        console.log(this.addCateForm)
        const { data: res } = await this.$http.post('categories', this.addCateForm)
        if (res.meta.status !== 201) return this.$message.error('添加分类失败')
        this.$message.success('添加分类成功')
        this.getCateList()
        this.addCategoryDialogVisible = false
      })
    },
    async showRemoveCateDialog (cateinfo) {
      this.removeCateDialogVisible = true
      this.getParentCate()
      const { data: res } = await this.$http.get('categories/' + cateinfo.cat_id)
      if (res.meta.status !== 200) return this.$message.error('信息获取失败')
      this.removeCateForm = res.data
      this.$message.success('信息获取成功')
    },
    removeCateDialogClosed () {
      this.$refs.removeCateFormRef.resetFields()
    },
    reviseCate () {
      this.$refs.removeCateFormRef.validate(async valid => {
        if (!valid) return
        const { data: res } = await this.$http.put('categories/' + this.removeCateForm.cat_id, this.removeCateForm)
        if (res.meta.status !== 200) return this.$message.error('修改失败')
        this.$message.success('修改成功')
        this.getCateList()
        this.removeCateDialogVisible = false
      })
    },
    async removeCate (id) {
      const confirmResult = await this.$confirm('此操作将永久删除该分类, 是否继续?', '删除分类', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).catch(error => error)
      if (confirmResult === 'confirm') {
        const { data: res } = await this.$http.delete('categories/' + id)
        if (res.meta.status !== 200) return this.$message.error('删除分类出错')
        this.$message.success('删除分类成功')
        this.getCateList()
      }
    }
  }
}
</script>

<style lang="less" scoped>
  .treeTable {
    margin-top: 15px;
  }
  .el-cascader {
    width: 100%;
  }
</style>
